<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title></title>
</head>

<script type="text/javascript" src="../js/jquery-1.9.1.min.js" th:src="@{static/js/jquery-1.9.1.min.js}"></script>
<script type="text/javascript" src="../js/bootstrap.min.js" th:src="@{static/js/bootstrap.min.js}"></script>

<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css" th:href="@{static/css/bootstrap.min.css}"/>
<link rel="stylesheet" href="../css/res/layui/css/layui.css" th:href="@{static/css/res/layui/css/layui.css}" media="all">
<script src="../layuiadmin/layui/layui.js" th:src="@{static/layuiadmin/layui/layui.js}"></script>
<script src="../js/ban.js" th:src="@{static/js/ban.js}"></script>
<script type='text/javascript' src='../js/cropper.min.js' th:src="@{static/js/cropper.min.js}"></script>
<link rel="stylesheet" type="text/css" href="../css/cropper.min.css" th:href="@{static/css/cropper.min.css}"/>
<link rel="stylesheet" type="text/css" href="../css/ImgCropping.css" th:href="@{static/css/ImgCropping.css}"/>
<style>
    body {
      height: 100%;
      overflow: hidden;
    }

    canvas {
      z-index: 0;
      position: absolute;
    }
 </style>
<body>
<canvas></canvas>
<a style="display:none;width:200px;height:42px;line-height:42px;font-size:18px;text-align:center;margin:40px auto;font-weight:800;" href="#" class="tbox">点击弹出</a>



<div class="modal fade" id="myModal">
<!--图片裁剪框 start-->
<div style="display: none" class="tailoring-container" >
    <div class="black-cloth" onclick="closeTailor(this)"></div>
    <div class="tailoring-content">
        <div class="tailoring-content-one">
           <!-- <label title="上传图片" for="chooseImg" class="l2-btn choose-btn">
                <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)">
                		本地上传
            </label>-->
            <label title="拍照" class="layui-btn layui-btn-normal" id='capture' style="margin-left: 2%;">拍照</label>
            <label title="重拍" class="layui-btn layui-btn-normal" id='takeAgain' style="margin-left: 2%;">重拍</label>
            <div class="close-tailoring"  onclick="closeTailor(this)">×</div>
        </div>
        <div class="tailoring-content-two">
            <div class="tailoring-box-parcel">
                <video id="video" width="100%" height="100%" controls  style="float: left;" ></video>
                <canvas id="canvas" width="482px" height="448px" style="float: left;" hidden="hidden"></canvas>
                <div id="showImg" hidden="hidden" style="width: 100%;height:100%;">
                    <img id="tailoringImg">
                </div>
            </div>
            <div class="preview-box-parcel">
                <p>图片预览：</p>
                <div class="square previewImg"></div>
                <div class="circular previewImg"></div>
            </div>
        </div>
        <div class="tailoring-content-three">
            <button class="layui-btn layui-btn-normal cropper-reset-btn">复位</button>
            <button class="layui-btn layui-btn-normal cropper-rotate-btn">旋转</button>
            <button class="layui-btn layui-btn-normal cropper-scaleX-btn">换向</button>
            <button class="layui-btn layui-btn-normal sureCut" id="sureCut">确定</button>
        </div>
    </div>
</div>
<!--图片裁剪框 end-->
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
					<h4 class="modal-title" align="center">*请在下面的框中输入口令进入考试*</h4>
					<br/>
					<form class="form-horizontal" role="form">
						<div class="form-group">
							<label for="name" class="col-sm-offset-2 col-sm-2 control-label">邮箱</label>
							<div class="col-sm-5">
								<input type="text" class="form-control" id="email" placeholder="请输您的邮箱">
							</div>
						</div>
						<div class="form-group">
							<label for="password" class="col-sm-offset-2 col-sm-2 control-label">邀请码</label>
							<div class="col-sm-5">
								<input type="text" class="form-control" id="invitecode" placeholder="请输入您的考试邀请码">
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-offset-4 col-sm-5">
								 <input type="text" id="base64url" style="display: none">
								<button id="VerifyFace" type="button" class="btn btn-default btn-block btn-primary">人脸身份验证</button>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-offset-4 col-sm-5">
								<button id="submitBtn" type="button" class="btn btn-default btn-block btn-primary">进入考试</button>
							</div>
						</div>
					</form>
			</div>
			<div class="modal-footer">
				<center>
				<div id="loading" style="display: none;">
					正在进行验证...
					<img src='../img/loading.gif' th:src='@{static/img/loading.gif}' />
				</div>
				</center>
			</div>
		</div>
		
	</div>
</div>

</body>


<script type="text/javascript">
window.onload = function(){ 
	//alert("text1");
	$(".tbox").trigger("click");
};
$(function(){
	//$(".tbox").trigger("click");
	
	$("#submitBtn").click(function(){
		var email = $('#email').val();
		var invitecode = $('#invitecode').val();
		//alert(email+invitecode);
		
		if($("#base64url").val()==""){
          	layui.use(['layer', 'form'], function(){
		  		var layer = layui.layer
		  			,form = layui.form;
		  			layer.msg('请上传人脸数据！');
			});
        }
		else
		{
			var base64url = $("#base64url").val();
			var datas={"email":email,"invitecode":invitecode,"base64url":base64url};
			$.ajax({
	        	type:"post",
	        	url:"verifyintoexam",
	        	async:true,
	        	dataType:'json',//'application/json;charset=UTF-8',//request请求的类型是json类型,字符编码是utf-8
				data:datas,//将json对象转换成字符串,再向后台传递
				beforeSend:function(XMLHttpRequest){ 
		          $("#loading").css("display","block"); //在后台返回success之前显示loading图标
		     	}, 
				success:function(e){
		         	$("#loading").css("display","none"); //ajax返回成功，清除loading图标
					if(e=="1")
					{
						layui.use(['layer', 'form'], function(){
					  		var layer = layui.layer
					  			,form = layui.form;
					  			layer.msg('身份验证成功！即将跳转到考试页面...');
						});
						parent.location.href = 'importpaper';
					}
					else if(e=="-1")
					{
						//parent.location.href = 'importpaper';//降低安全性操作
						layui.use(['layer', 'form'], function(){
					  		var layer = layui.layer
					  			,form = layui.form;
					  			layer.msg('人脸不匹配！请重新录入人脸数据！');
						});
					}
					else if(e=="-2")
					{
						//parent.location.href = 'importpaper';//降低安全性操作
						layui.use(['layer', 'form'], function(){
					  		var layer = layui.layer
					  			,form = layui.form;
					  			layer.msg('系统没有检测到人脸！请重新录入人脸数据！');
						});
					}
					else if(e=="-3")
					{
						layui.use(['layer', 'form'], function(){
					  		var layer = layui.layer
					  			,form = layui.form;
					  			layer.msg('身份验证失败！');
						}); 
						
					}
					else if(e=="0")
					{
						layui.use(['layer', 'form'], function(){
					  		var layer = layui.layer
					  			,form = layui.form;
					  			layer.msg('当前时间不在考试时间范围内！');
						});
					}
				},
				error:function(e){
					console.log(e);
					layui.use(['layer', 'form'], function(){
				  		var layer = layui.layer
				  			,form = layui.form;
				  			layer.msg('服务器开小差了...');
					});
				}
	        });
		}
		
	})
	
	$(".tbox").click(function(){
		$('#myModal').modal('show'); //显示模态框
	})
	
});
</script>
<script>

    //弹出框水平垂直居中
    (window.onresize = function () {
        var win_height = $(window).height();
        var win_width = $(window).width();
        if (win_width <= 768){
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": 0
            });
        }else{
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": (win_width - $(".tailoring-content").outerWidth())/2
            });
        }
    })();

    //图像上传
    function selectImg(file) {
        if (!file.files || !file.files[0]){
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            var replaceSrc = evt.target.result;
            //更换cropper的图片
            $('#tailoringImg').cropper('replace', replaceSrc,false);//默认false，适应高度，不失真

        }
        reader.readAsDataURL(file.files[0]);
        mediaStreamTrack && mediaStreamTrack.stop();
        $("#video").hide();
        $("#showImg").show();

    }
    //cropper图片裁剪
    $('#tailoringImg').cropper({
        aspectRatio: 1/1,//默认比例
        preview: '.previewImg',//预览视图
        guides: false,  //裁剪框的虚线(九宫格)
        autoCropArea: 0.5,  //0-1之间的数值，定义自动剪裁区域的大小，默认0.8
        movable: false, //是否允许移动图片
        dragCrop: true,  //是否允许移除当前的剪裁框，并通过拖动来新建一个剪裁框区域
        movable: true,  //是否允许移动剪裁框
        resizable: true,  //是否允许改变裁剪框的大小
        zoomable: false,  //是否允许缩放图片大小
        mouseWheelZoom: false,  //是否允许通过鼠标滚轮来缩放图片
        touchDragZoom: true,  //是否允许通过触摸移动来缩放图片
        rotatable: true,  //是否允许旋转图片
        crop: function(e) {
            // 输出结果数据裁剪图像。
        }
    });

    //弹框
    $("#VerifyFace").on("click",function () {
        takeImg()
    });

    //旋转
    $(".cropper-rotate-btn").on("click",function () {
        $('#tailoringImg').cropper("rotate", 45);
    });
    //复位
    $(".cropper-reset-btn").on("click",function () {
        $('#tailoringImg').cropper("reset");
    });
    //换向
    var flagX = true;
    $(".cropper-scaleX-btn").on("click",function () {
        if(flagX){
            $('#tailoringImg').cropper("scaleX", -1);
            flagX = false;
        }else{
            $('#tailoringImg').cropper("scaleX", 1);
            flagX = true;
        }
        flagX != flagX;
    });

    //裁剪后的处理
    $("#sureCut").on("click",function () {
        var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas
        var base64url = cas.toDataURL('image/png'); //转换为base64地址形式
        base64url=base64url.replace("\r","");
        $("#base64url").val(base64url);
        var datas={"base64url":base64url}
        $.ajax({
			url:'doPhoto',
			type:'post',
			dataType:'json',
			async:true,
			data:datas,
			success:function(e){
				alert(e);
				//window.location.href = 'toExamOver';
			},
			error:function(e){
				console.log(e);
				layui.use(['layer', 'form'], function(){
			  		var layer = layui.layer
			  			,form = layui.form;
			  			layer.msg('保存成功！');
				});
			}
		});
        
        closeTailor();
    });
    //关闭裁剪框
    function closeTailor() {
        $(".tailoring-container").toggle();
        mediaStreamTrack && mediaStreamTrack.stop();
    }

    //访问用户媒体设备的兼容方法
    function getUserMedia(constraints, success, error) {
        if (navigator.mediaDevices.getUserMedia) {
            //最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        } else if (navigator.webkitGetUserMedia) {
            //webkit核心浏览器
            navigator.webkitGetUserMedia(constraints,success, error)
        } else if (navigator.mozGetUserMedia) {
            //firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        } else if (navigator.getUserMedia) {
            //旧版API
            navigator.getUserMedia(constraints, success, error);
        }
    }

    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let context = canvas.getContext('2d');
    var mediaStreamTrack
    function success(stream) {
        //兼容webkit核心浏览器
        let CompatibleURL = window.URL || window.webkitURL;
        //将视频流设置为video元素的源
        mediaStreamTrack = stream.getTracks()[0];
        //video.src = CompatibleURL.createObjectURL(stream);
        video.srcObject = stream;
        video.play();
    }

    function error(error) {
        alert('访问用户媒体设备失败,请尝试更换浏览器')
    }



    document.getElementById('capture').addEventListener('click', function () {
        context.drawImage(video, 0, 0, 480, 320);
        mediaStreamTrack && mediaStreamTrack.stop();
        $('#tailoringImg').cropper('replace', canvas.toDataURL("image/png"),false);//默认false，适应高度，不失真
        $("#video").hide();//隐藏拍照框
        $("#showImg").show()//将拍照结果显示
    })

    //请求拍照
    $("#takeAgain").bind("click", function(){
        takePhoto();
    });

    //开始拍照
    function takeImg(){
        $(".tailoring-container").toggle();
        takePhoto();
    }

    //请求摄像头
    function takePhoto() {
        if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
            //调用用户媒体设备, 访问摄像头
            getUserMedia({video : {width: 100, height: 100}}, success, error);
            $("#showImg").hide();//隐藏拍照结果显示框
            //$('#showImg').html('<img id="tailoringImg" hidden="hidden">')
            $("#video").show();//开启拍照框
        } else {
            alert('不支持访问用户媒体');
        }
    }
</script>
</html>